home *** CD-ROM | disk | FTP | other *** search
/ Graphics & Sound Program…ng Techniques for the Mac / Graphics and Sound Programming Techniques for the Mac.iso / M&T Graphics & Sound Examples / Metrowerks Versions / C02 Sound Playing / P03 Sound Commands / SoundCommands.c next >
Encoding:
C/C++ Source or Header  |  1995-08-05  |  3.4 KB  |  153 lines  |  [TEXT/MMCC]

  1. //____________________________________________________________
  2. //    SoundCommands.c
  3. //
  4. //    Copyright © Dan Parks Sydow, 1995
  5. //    From the book:
  6. //    "Graphics and Sound Programming Techniques for the Mac",
  7. //    M&T Books, 1995
  8.  
  9.  
  10. //____________________________________________________________
  11.  
  12. #include <Sound.h>
  13.  
  14.  
  15. //____________________________________________________________
  16.  
  17. void           InitializeToolbox( void );
  18. SndChannelPtr  OpenOneSynchSoundChannel( void );
  19. OSErr          DisposeOneSoundChannel( SndChannelPtr );
  20. OSErr          PlaySoundResourceSynch( SndChannelPtr, short );
  21. OSErr          SetSoundAmplitude( SndChannelPtr, short );
  22.  
  23. //____________________________________________________________
  24.  
  25. #define    rPoliceSiren      9000
  26.  
  27.  
  28. //____________________________________________________________
  29.  
  30. void  main( void )
  31. {
  32.    NumVersion     theSndMgrVers;
  33.    short          theResID;
  34.    OSErr          theError;
  35.    SndChannelPtr  theChannel;
  36.    short          theAmplitude;
  37.       
  38.    InitializeToolbox();
  39.  
  40.    theSndMgrVers = SndSoundManagerVersion();   
  41.    if ( theSndMgrVers.majorRev < 3 )
  42.       ExitToShell();
  43.    
  44.    theChannel = OpenOneSynchSoundChannel();
  45.    if ( theChannel == nil )
  46.       ExitToShell();
  47.  
  48.    theAmplitude = 50;
  49.    theError = SetSoundAmplitude( theChannel, theAmplitude );
  50.    if ( theError != noErr )
  51.       ExitToShell();
  52.  
  53.    theResID = rPoliceSiren;
  54.    theError = PlaySoundResourceSynch( theChannel, theResID );
  55.    if ( theError != noErr )
  56.       ExitToShell();
  57.    
  58.    theError = DisposeOneSoundChannel( theChannel );
  59.    if ( theError != noErr )
  60.       ExitToShell();
  61. }
  62.  
  63.  
  64. //____________________________________________________________
  65.  
  66. OSErr  SetSoundAmplitude( SndChannelPtr theChannel, short theAmp )
  67. {
  68.    SndCommand  theCommand;
  69.    OSErr       theError;
  70.    
  71.    theCommand.cmd = ampCmd;
  72.    theCommand.param1 = theAmp;
  73.    theCommand.param2 = 0;
  74.    
  75.    theError = SndDoCommand( theChannel, &theCommand, false );
  76.    
  77.    return ( theError );
  78. }
  79.  
  80.  
  81. //____________________________________________________________
  82.  
  83. SndChannelPtr  OpenOneSynchSoundChannel( void )
  84. {
  85.    SndChannelPtr  theChannel;   
  86.    OSErr          theError;
  87.    
  88.    theChannel = nil;
  89.    theError = SndNewChannel( &theChannel, 0, 0, nil );
  90.    
  91.    if ( theError != noErr )
  92.    {
  93.       DisposePtr( (Ptr)theChannel );
  94.       theChannel = nil;
  95.    }
  96.       
  97.    return ( theChannel );
  98. }
  99.  
  100.  
  101. //____________________________________________________________
  102.  
  103. OSErr  DisposeOneSoundChannel( SndChannelPtr theChannel )
  104. {
  105.    OSErr  theError;
  106.    
  107.    theError = SndDisposeChannel( theChannel, true );
  108.    DisposePtr( (Ptr)theChannel );
  109.    
  110.    return ( theError );
  111. }
  112.  
  113.  
  114. //____________________________________________________________
  115.  
  116. OSErr  PlaySoundResourceSynch( SndChannelPtr theChannel, short theResID )
  117. {
  118.    Handle  theHandle;
  119.    OSErr   theError;
  120.    
  121.    theHandle = GetResource( 'snd ', theResID );
  122.    
  123.    if ( theHandle == nil )
  124.    {   
  125.       return ( resProblem );
  126.    }
  127.    else
  128.    {
  129.       HLock( theHandle );
  130.          theError = SndPlay( theChannel, (SndListHandle)theHandle, false );
  131.       HUnlock( theHandle );
  132.    
  133.       ReleaseResource( theHandle );
  134.  
  135.       return ( theError );
  136.    }
  137. }
  138.  
  139.  
  140. //____________________________________________________________
  141.  
  142. void  InitializeToolbox( void )
  143. {
  144.    InitGraf( &qd.thePort );
  145.    InitFonts();
  146.    InitWindows();
  147.    InitMenus();
  148.    TEInit();
  149.    InitDialogs( 0L );
  150.    FlushEvents( everyEvent, 0 );
  151.    InitCursor();
  152. }
  153.